package cn.hhhy.controller;

import cn.hhhy.pojo.*;
import cn.hhhy.service.CouponService;
import cn.hhhy.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private OrderService orderService;

    // 分页查询
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean pageBean) {
        return orderService.findPage(pageBean);
    }
    // 根据id查询
    @RequestMapping("/findById")
    public Result findById(Integer id) {
        try {
            Order order = orderService.findById(id);
            return new Result(true,"查询成功",order);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"查询失败");
        }
    }

    // 添加
    @RequestMapping("/add")
    public Result add(@RequestBody Order order) {
        try {
            orderService.add(order);
            return new Result(true,"添加成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"添加失败");
        }
    }
    // 删除
    @RequestMapping("/delete")
    public Result delete(Integer id) {
        try {
            orderService.delete(id);
            return new Result(true,"删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"删除失败");
        }
    }
    // 修改
    @RequestMapping("/update")
    public Result update(@RequestBody Order order) {
        try {
            orderService.update(order);
            return new Result(true,"更新成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"更新失败");
        }

    }
    // 用户购买生成订单
    @RequestMapping("/putOrder")
    public Result putOrder(@RequestBody Map<String, Object> orderInfo) {
        try {
            orderService.putOrder(orderInfo);
            return new Result(true,"订单创建成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"订单生成失败");
        }
    }

    // 根据用户id查询订单
    @RequestMapping("/findByUID")
    public Result findByUID(Integer id) {
        try {
            List<Order> orders =  orderService.findByUID(id);
            return new Result(true,"获取成功",orders);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"获取失败");
        }
    }

    //确认支付
    @RequestMapping("/pay")
    public Result pay(Integer id) {
        try {
            Order o = orderService.pay(id);
            return new Result(true, "支付成功",o);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"支付失败");
        }
    }
}
